using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

[ExecuteInEditMode, RequireComponent(typeof(TrainResPipeLine))]
public class TrainResConnector : MonoBehaviour {

    public bool test;
	public List<Transform> list = new List<Transform>();
    Transform selectedTrans;
    Vector3 selectedLastPos;
	
	// Use this for initialization
	void Start () {
        GameObject go = new GameObject();
        go.transform.parent = this.transform;
        list.Add(go.transform);
    }

    void OnGUI()
    {

    }

	// Update is called once per frame
	void Update () {
		if(test)
		{
			test = false;
			buildPipeLine(true);
		}

        if (Selection.activeGameObject != null)
        {
            selectedTrans = Selection.activeGameObject.transform;
            if (selectedTrans != null && selectedTrans.position != selectedLastPos)
            {
                buildPipeLine(false);
                selectedLastPos = selectedTrans.position;
            }
        }
	}

    private void buildPipeLine(bool showAnimation)
	{
        list.Clear();
        for (int i = 0; i < transform.GetChildCount(); i++)
        {
            Transform t = transform.GetChild(i);
            t.name = "marker_" + i;
            list.Add(t);
        }

        TrainResPipeLine pl = GetComponent<TrainResPipeLine>();
		if(pl != null)
		{
			pl.markers.Clear();
			pl.markers.AddRange(list);
            pl.showAnimation = showAnimation;
			pl.refresh();
		}
	}
}
